-- slime_status
-- created by zhuangsl
-- 冈布奥主动技能附加通用状态
-- 格式：common_status(id, status, prop_id, round, value, value2,……)
-- id对应slime_skill技能编号，status是状态id，prop_id是属性id（无则写0），round是有效回合（-1表示永久起效），value、value2……是状态数值

return {
    apply = function(source, target, skillId, round, para, extra)
        -- 专属技能等级
        local level;
        local prop;
        extra = extra or {};
        if extra["level"] then
            level = tonumber(extra["level"]);
        else
            prop = PropM.combine(source, "gain_slime_skill", para[1]);
            level = PropM.apply(prop, 0);
        end

        -- 持续时间
        local effectRound = -1;
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[4] > 0 then
            effectRound = round + para[4] + 1;
        end

        -- 状态参数
        local condition = {
            ["end_round"] = effectRound,
        };

        -- 如果有prop_id，配置为0时不记录
        if para[3] > 0 then
            condition["id"] = para[3];
        end

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);
        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end

        -- 效果数值，每一条的实际数值 = 系数 * level
        local values = para[5]
        local value;

        local valueName = "value"
        for i = 1, #values do
            if i > 1 then
                valueName = "value" .. i;
            end

            value = values[i] * level;
            if value ~= 0 then
                value = PropM.apply(prop, value);
                condition[valueName] = value;
            end
        end

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 如果不免疫该技能，附加状态
        if SkillM.immunitySkill(source, target, skillId) then
            SkillM.getSequence(target):immunity(source, target, skillId);
        else
            CombatStatusM.applyStatus(target, para[2], condition);
        end

        -- 返回作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        local values = string.explode(arr[5], "+");
        for index, value in pairs(values) do
            values[index] = tonumber(value);
        end

        return { tonumber(arr[1]), tonumber(arr[2]), tonumber(arr[3]), tonumber(arr[4]), values };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 专属技能等级
        local prop = PropM.combine(source, "gain_slime_skill", para[1]);
        local level = PropM.apply(prop, 0);

        -- 斯巴达专属加成
        prop = PropM.combine(source, "battle_cry", 1);

        local values = para[5];
        local value;
        for i = 1, #values do
            value = values[i] * level;
            if value ~= 0 then
                value = PropM.apply(prop, value);
                -- TODO:  百分比类的没法分辨，先特殊处理一下
                if para[1] == 26 and i == 1 then
                    value = math.modf(value / 10);
                end
                if para[1] == 33 then
                    value = math.modf(math.abs(value) / 10);
                end
            end
            desc = string.gsub(desc, "{value}", value, 1);
        end

        local round = para[4];
        desc = string.gsub(desc, "{round}", round);

        return desc;
    end,
};
